package com.dmwork.service.work.controller.front.me;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmwork.common.base.result.R;
import com.dmwork.service.base.model.FileUrlVo;
import com.dmwork.service.work.entity.Work;
import com.dmwork.service.work.entity.WorkPictureInfo;
import com.dmwork.service.work.feign.OssFileFeignService;
import com.dmwork.service.work.service.WorkPictureInfoService;
import com.dmwork.service.work.service.WorkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Api(description = "作品图片管理")
@Transactional
@RestController
@RequestMapping("/front/dm_work/work/work_picture/me")
public class FrontMeWorkPictureInfoController {

    @Autowired
    private WorkPictureInfoService workPictureInfoService;
    @Autowired
    private WorkService workService;
    @Autowired
    private OssFileFeignService ossFileFeignService;

    @ApiOperation("添加展示图片信息")
    @PostMapping("add_info")
    public R addInfo(@ApiParam(value = "添加的展示图片信息", required = true) @RequestBody WorkPictureInfo workPictureInfo) {
        workPictureInfoService.save(workPictureInfo);
        //更新work表中的picture_count
        Work work = workService.getById(workPictureInfo.getWorkId());
        work.setPictureCount(work.getPictureCount() + 1);
        workService.updateById(work);
        return R.ok().message("添加成功");
    }

    @ApiOperation("修改图片信息")
    @PutMapping("update_info")
    public R updateInfo(@ApiParam(value = "图片信息", required = true) @RequestBody WorkPictureInfo workPictureInfo) {
        workPictureInfoService.updateById(workPictureInfo);
        return R.ok().message("修改成功");
    }

    @ApiOperation("获取指定作品的图片信息")
    @GetMapping("get_info_list/{workId}")
    public R getInfoList(@ApiParam @PathVariable("workId") String workId) {
        QueryWrapper<WorkPictureInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("work_id", workId);
        List<WorkPictureInfo> workPictureInfoList = workPictureInfoService.list(queryWrapper);
        return R.ok().data("items", workPictureInfoList);
    }

    @ApiOperation("删除指定图片信息")
    @DeleteMapping("remove/{pictureId}")
    public R remove(@ApiParam(value = "图片id") @PathVariable("pictureId") String pictureId) {
        WorkPictureInfo workPictureInfo = workPictureInfoService.getById(pictureId);
        //获取作品id
        String workId = workPictureInfo.getWorkId();
        //获取展示图片url
        String pictureUrl = workPictureInfo.getPictureUrl();

        //更新作品表的picture_count
        Work work = workService.getById(workId);
        work.setPictureCount(work.getPictureCount() - 1);
        workService.updateById(work);
        //删除数据库图片信息
        workPictureInfoService.removeById(pictureId);

        //远程调用OSS微服务删除图片资源
        ossFileFeignService.removeFile(pictureUrl);

        return R.ok().message("删除成功");
    }

    @ApiOperation("批量删除图片信息")
    @DeleteMapping(value = "batch_remove/{workId}")
    public R batchRemove(@PathVariable String workId, @RequestBody List<WorkPictureInfo> workPictureInfoList) {
        log.info(Arrays.toString(workPictureInfoList.toArray()));
        //图片id列表
        List<String> idList = new ArrayList<>();
        //图片url列表
        List<String> pictureUrlList = new ArrayList<>();
        for (WorkPictureInfo workPictureInfo : workPictureInfoList) {
            idList.add(workPictureInfo.getId());
            pictureUrlList.add(workPictureInfo.getPictureUrl());
        }
        log.info(Arrays.toString(idList.toArray()));
        log.info(Arrays.toString(pictureUrlList.toArray()));
        //删除数据库中的图片信息
        workPictureInfoService.removePictureInfoByIds(workId, idList);
        //删除oss中的图片信息
        new Thread(() -> {
            ossFileFeignService.removeBatchFile(FileUrlVo.builder().urlList(pictureUrlList).build());
        }).start();
        return R.ok().message("删除成功");
    }

}

